//
//  GameGlobal.cpp
//  Lnn
//
//  Created by Wu Wang on 12-3-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#include "GameGlobal.h"

using namespace cocos2d;

CGameGlobal *CGameGlobal::m_pInstance;

CGameGlobal *CGameGlobal::GetInstance()
{
	if(NULL == m_pInstance)
	{
		m_pInstance = new CGameGlobal;
	}

	return m_pInstance;
}

//---------------------------------------------------------------------------------------------------/
CCPoint CGameGlobal::AdjustPos(cocos2d::CCPoint pt)
{
	return ccp(pt.x * CGameGlobal::GetInstance()->m_scaleX, pt.y * CGameGlobal::GetInstance()->m_scaleY);
}

//---------------------------------------------------------------------------------------------------/
float CGameGlobal::AdjustX(float x)
{
	return x * CGameGlobal::GetInstance()->m_scaleX;
}

//---------------------------------------------------------------------------------------------------/
float CGameGlobal::AdjustY(float y)
{
	return y * CGameGlobal::GetInstance()->m_scaleY;
}

//---------------------------------------------------------------------------------------------------/
CCSize CGameGlobal::AdjustSize(cocos2d::CCSize s)
{
	return CCSize(s.width * CGameGlobal::GetInstance()->m_scaleX, s.height * CGameGlobal::GetInstance()->m_scaleY);
}

//---------------------------------------------------------------------------------------------------/
CCRect CGameGlobal::AdjustRect(cocos2d::CCRect rc)
{
	return CCRect(rc.origin.x * CGameGlobal::GetInstance()->m_scaleX, rc.origin.y * CGameGlobal::GetInstance()->m_scaleY,
				  rc.size.width * CGameGlobal::GetInstance()->m_scaleX, rc.size.height *CGameGlobal::GetInstance()->m_scaleY);
}

//---------------------------------------------------------------------------------------------------/
CCPoint CGameGlobal::AdjustResPos(cocos2d::CCPoint pt)
{
	return ccp(CGameGlobal::GetInstance()->m_resScale * pt.x, CGameGlobal::GetInstance()->m_resScale * pt.y);
}

//---------------------------------------------------------------------------------------------------/
float   CGameGlobal::AdjustResX(float x)
{
	return CGameGlobal::GetInstance()->m_resolutionScale * x * CGameGlobal::GetInstance()->m_scaleX;
}

//---------------------------------------------------------------------------------------------------/
float   CGameGlobal::AdjustResY(float y)
{
	return CGameGlobal::GetInstance()->m_resolutionScale * y * CGameGlobal::GetInstance()->m_scaleY;
}

//---------------------------------------------------------------------------------------------------/
CCSize  CGameGlobal::AdjustResSize(cocos2d::CCSize s)
{
	return CCSize( s.width*CGameGlobal::GetInstance()->m_resScale,  s.height*CGameGlobal::GetInstance()->m_resScale);
}

//---------------------------------------------------------------------------------------------------/
CCRect  CGameGlobal::AdjustResRect(cocos2d::CCRect rc)
{
	return CCRect(CGameGlobal::GetInstance()->m_resolutionScale * rc.origin.x * CGameGlobal::GetInstance()->m_scaleX, 
		CGameGlobal::GetInstance()->m_resolutionScale * rc.origin.y * CGameGlobal::GetInstance()->m_scaleY,
		CGameGlobal::GetInstance()->m_resolutionScale * rc.size.width * CGameGlobal::GetInstance()->m_scaleX, 
		CGameGlobal::GetInstance()->m_resolutionScale * rc.size.height * CGameGlobal::GetInstance()->m_scaleY);
}

void CGameGlobal::AdjustAndroid(cocos2d::CCLayer *pLayer, float scaleAddX, float scaleAddY)
{
	CCSize screenSize = CCDirector::sharedDirector()->getWinSize();

	float width = CGameGlobal::GetInstance()->m_width;
	float height = CGameGlobal::GetInstance()->m_height;

	
	pLayer->setAnchorPoint(ccp(0, 0));
	//pLayer->setPosition(ccp((screenSize.width - width) * 0.5f,(screenSize.height - height) * 0.5f));
	pLayer->setScaleX(screenSize.width / width + scaleAddX);
	pLayer->setScaleY(screenSize.height / height + scaleAddY);
}
//---------------------------------------------------------------------------------------------------/
CCPoint CGameGlobal::RestoreLogicPos(cocos2d::CCPoint pt)
{
	return ccp(pt.x / CGameGlobal::GetInstance()->m_scaleX, pt.y / CGameGlobal::GetInstance()->m_scaleY);
}

CCPoint CGameGlobal::RestoreResToLogicPos(cocos2d::CCPoint pt)
{
	return ccp(pt.x / CGameGlobal::GetInstance()->m_resScale, pt.y / CGameGlobal::GetInstance()->m_resScale);
}

//---------------------------------------------------------------------------------------------------/
float CGameGlobal::RestoreLogicX(float x)
{
	return x / CGameGlobal::GetInstance()->m_scaleX;
}

//---------------------------------------------------------------------------------------------------/
float CGameGlobal::RestoreLogicY(float y)
{
	return y /CGameGlobal::GetInstance()->m_scaleY;
}

//---------------------------------------------------------------------------------------------------/

float CGameGlobal::GetScaleX()
{
	return CGameGlobal::GetInstance()->m_scaleX;
}

//---------------------------------------------------------------------------------------------------/
float CGameGlobal::GetScaleY()
{
	return CGameGlobal::GetInstance()->m_scaleY;
}

float CGameGlobal::GetResScaleX()
{
	return CGameGlobal::GetInstance()->m_resScaleX;
}

//---------------------------------------------------------------------------------------------------/
float CGameGlobal::GetResScaleY()
{
	return CGameGlobal::GetInstance()->m_resScaleY;
}

//---------------------------------------------------------------------------------------------------/
float CGameGlobal::GetResScale()
{
	return CGameGlobal::GetInstance()->m_resScale;
}

float CGameGlobal::GetResolutionScale()
{
	return CGameGlobal::GetInstance()->m_resolutionScale;
}
//---------------------------------------------------------------------------------------------------/
void CGameGlobal::InitGlobalData(float logicWidth, float logicHeight, float resScaleFactor, float screenWidth, float screenHeight, bool bRetain)
{
	cocos2d::CCSize s = cocos2d::CCDirector::sharedDirector()->getWinSize();
	if(-1 != screenWidth && -1 != screenHeight)
	{
		s.width = screenWidth;
		s.height = screenHeight;
	}
	
	m_width = s.width;
	m_height = s.height;


	m_centerDiffX = (SCREEN_WIDTH - m_width) * 0.5f;
	m_centerDiffY = (SCREEN_HEIGHT - m_height) *0.5f;

	m_logicWidth = logicWidth;
	m_logicHeight = logicHeight;



	assert(m_resScale != 0);
	m_resScale = resScaleFactor;

	if(bRetain)
	{
		m_resolutionScale = 2.0f;
	}

	//m_resolutionScale = 2.0f;
	assert(logicWidth >= 1&& logicHeight >= 1);
	m_scaleX = m_width / m_logicWidth;
	m_scaleY = m_height / m_logicHeight;

	m_resScaleX = m_scaleX / m_resScale;
	m_resScaleY = m_scaleY / m_resScale;
    
    m_ptCenter = ccp(m_width * 0.5f, m_height * 0.5f);
}
